package com.ssyc.common.quanzi;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.utils.TextUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ssyc.common.R;
import com.ssyc.common.base.BaseActivity;
import com.ssyc.common.base.BaseApplication;
import com.ssyc.common.base.Constants;
import com.ssyc.common.base.SpKeys;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.common.http.HttpUtils;
import com.ssyc.common.manager.AccountUtils;
import com.ssyc.common.utils.GsonUtil;
import com.ssyc.common.utils.SPUtil;
import com.ssyc.common.utils.UiUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * 圈子头像点击进入的公共的个人资料界面
 * Created by Administrator on 2018/7/10 0010.
 */
@Route(path = "/quanzi/PersonInfoActivity")
public class PersonInfoActivity extends BaseActivity implements View.OnClickListener {

    private ImageView iv_back;
    private CircleImageView ivHead;
    private TextView tvName;
    private TextView tvTel;
    private RecyclerView rv;
    private LinearLayout llPersoninfo;
    private LinearLayout llConent;
    private RelativeLayout rlLoading;
    private View line;

    private QuanZiInfo.CirclelistBean info;

    private PersonInfo.DataBean infoData;

    //表示是从云信的单聊界面顶部的个人资料跳转过来的
    private String sessionId;

    @Override
    protected boolean isNeedShowTitle() {
        return false;
    }

    @Override
    protected String setTitleText() {
        return null;
    }

    @Override
    protected int getContentLayout() {
        return R.layout.base_activity_person_info;
    }

    @Override
    protected void init() {

        showContent();

        //获取从好友头像点击进入的个人资料时好友的数据模型
        initIntent();


        //初始化视图
        initView();

        //请求页面数据
        http();
    }

    /**
     * 获取点击好友头像进入个人资料时的好友的数据模型
     */
    private void initIntent() {
        info = (QuanZiInfo.CirclelistBean) getIntent().getSerializableExtra("data");
        sessionId = getIntent().getStringExtra("accid");
    }


    /**
     * 初始化视图
     */
    public void initView() {
        iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(this);
        ivHead = findViewById(R.id.iv_head);
        tvName = findViewById(R.id.tv_name);
        tvTel = findViewById(R.id.tv_tel);
        rv = findViewById(R.id.rv);
        llPersoninfo = findViewById(R.id.ll_personinfo);
        llPersoninfo.setOnClickListener(this);
        llConent = findViewById(R.id.ll_conent);
        llConent.setVisibility(View.GONE);
        rlLoading = findViewById(R.id.rl_loading);
        rlLoading.setVisibility(View.VISIBLE);
        line = findViewById(R.id.line);

        if("0".equals(AccountUtils.getRole(this))){
            llPersoninfo.setVisibility(View.GONE);
            line.setVisibility(View.GONE);
        }
    }


    /**
     * 请求页面数据
     */
    private void http() {
        Map<String, String> map = new HashMap<>();
        map.put("type", "13");
        map.put("platform", "8");
        map.put("apptoken", AccountUtils.getTestToken(this));
        if (!TextUtils.isEmpty(sessionId)) {
            //表示是从云信聊天界面跳转过来的
            map.put("acc", sessionId.substring(0, sessionId.length() - 1));
            map.put("role", sessionId.substring(sessionId.length() - 1));
        } else {
            //表示不是从云信聊天界面跳转过来的
            if (1 == SPUtil.getInt(this, SpKeys.CIRCLETYPE)) {
                //表示是自己的个人资料
                map.put("acc", AccountUtils.getAccount(this));
                map.put("role", AccountUtils.getRole(this));
            } else if (2 == SPUtil.getInt(this, SpKeys.CIRCLETYPE)) {
                //表示是好友的个人资料
                map.put("acc", info.getUser_id() + "");
                map.put("role", info.getRole() + "");
            }
        }


        HttpUtils.post(HttpAdress.CIRCLEPERSONINFO, map, this, new HttpUtils.CallBack() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }
                UiUtils.Toast(BaseApplication.ERROR, false);
            }

            @Override
            public void onResponse(String response, int id) {
                if (rlLoading != null && rlLoading.getVisibility() == View.VISIBLE) {
                    rlLoading.setVisibility(View.GONE);
                }

                if (llConent != null && llConent.getVisibility() != View.VISIBLE) {
                    llConent.setVisibility(View.VISIBLE);
                }

                if (!TextUtils.isEmpty(response)) {
                    PersonInfo info = null;
                    try {
                        info = GsonUtil.jsonToBean(response, PersonInfo.class);
                    } catch (Exception e) {
                        Log.i("test", Constants.PARSEDATAERROR);
                    }

                    if (null != info) {
                        if ("200".equals(info.getState())) {
                            //返回成功
                            //设置页面数据
                            infoData = info.getData();
                            if (null != infoData) {
                                initData();
                            }
                        } else {
                            UiUtils.Toast(Constants.ERRORSTATE + info.getState(), false);
                            Log.i("test", Constants.ERRORSTATE + info.getState());
                        }

                    } else {
                        Log.i("test", Constants.EMPTY);
                    }

                } else {
                    Log.i("test", Constants.EMPTY);
                }

            }
        });
    }


    /**
     * 设置页面数据
     */
    private void initData() {

        //设置头像
        ImageUtil.displayImage(this, ivHead, infoData.getStudent_icon());

        //设置姓名
        if (TextUtils.isEmpty(infoData.getStudent_name())) {
            tvName.setText("暂无姓名");
        } else {
            tvName.setText(infoData.getStudent_name());
        }

        //设置电话
        if (TextUtils.isEmpty(infoData.getUser_id())) {
            tvTel.setText("暂无电话");
        } else {
            if ("1".equals(SPUtil.getString(this, SpKeys.DISABLEGROUP))) {
                if (!infoData.getUser_id().equals(AccountUtils.getAccount(this))) {
                    String tel = infoData.getUser_id();
                    String str = "****";
                    StringBuilder sb = new StringBuilder(tel);
                    sb.replace(3, 7, str);
                    tvTel.setText(sb.toString());
                } else {
                    tvTel.setText(infoData.getUser_id());
                }
            } else {
                tvTel.setText(infoData.getUser_id());
            }
        }

        //设置相册的图片
        List<String> oldDatas = new ArrayList<>();
        if (!TextUtils.isEmpty(infoData.getPicture1())) {
            oldDatas.add(infoData.getPicture1());
        }

        if (!TextUtils.isEmpty(infoData.getPicture2())) {
            oldDatas.add(infoData.getPicture2());
        }

        if (!TextUtils.isEmpty(infoData.getPicture3())) {
            oldDatas.add(infoData.getPicture3());
        }

        if (oldDatas.size() != 0) {
            rv.setVisibility(View.VISIBLE);
            LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false);
            rv.setLayoutManager(manager);
            PicRvAdapter adapter = new PicRvAdapter(this, R.layout.base_rv_circle, oldDatas);
            rv.setAdapter(adapter);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    //跳转到个人的圈子界面

                    Intent intent = new Intent(PersonInfoActivity.this,
                            PersonCircleActivity.class);
                    if (!TextUtils.isEmpty(sessionId)) {
                        //表示是从云信聊天界面跳转过来的
                        intent.putExtra("acc2", sessionId.substring(0, sessionId.length() - 1));
                        intent.putExtra("role2", sessionId.substring(sessionId.length() - 1));
                        intent.putExtra("name", tvName.getText().toString());
                        intent.putExtra("icon", infoData.getStudent_icon());
                    } else {
                        if (2 == SPUtil.getInt(PersonInfoActivity.this, SpKeys.CIRCLETYPE)) {
                            //表示是好友的个人资料
                            intent.putExtra("acc2", info.getUser_id() + "");
                            intent.putExtra("role2", info.getRole() + "");
                            intent.putExtra("name", tvName.getText().toString());
                            intent.putExtra("icon", infoData.getStudent_icon());

                        } else if (1 == SPUtil.getInt(PersonInfoActivity.this, SpKeys.CIRCLETYPE)) {
                            //表示是自己的个人资料
                            intent.putExtra("acc2", AccountUtils.getAccount(PersonInfoActivity.this));
                            intent.putExtra("role2", AccountUtils.getRole(PersonInfoActivity.this));
                            intent.putExtra("name", AccountUtils.getName(PersonInfoActivity.this));
                            intent.putExtra("icon", AccountUtils.getIcon(PersonInfoActivity.this));
                        }
                    }
                    startActivity(intent);
                }
            });
        } else {
            rv.setVisibility(View.INVISIBLE);
        }
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            //返回
            finish();
        } else if (id == R.id.ll_personinfo) {
            //跳转到个人的圈子界面
            Intent intent = new Intent(PersonInfoActivity.this,
                    PersonCircleActivity.class);

            if (!TextUtils.isEmpty(sessionId)) {
                //表示是从云信聊天界面跳转过来的
                intent.putExtra("acc2", sessionId.substring(0, sessionId.length() - 1));
                intent.putExtra("role2", sessionId.substring(sessionId.length() - 1));
                intent.putExtra("name", tvName.getText().toString());
                intent.putExtra("icon", infoData.getStudent_icon());
            } else {
                if (2 == SPUtil.getInt(PersonInfoActivity.this, SpKeys.CIRCLETYPE)) {
                    //表示是好友的个人资料
                    intent.putExtra("acc2", info.getUser_id() + "");
                    intent.putExtra("role2", info.getRole() + "");
                    intent.putExtra("name", tvName.getText().toString());
                    intent.putExtra("icon", infoData.getStudent_icon());

                } else if (1 == SPUtil.getInt(PersonInfoActivity.this, SpKeys.CIRCLETYPE)) {
                    //表示是自己的个人资料
                    intent.putExtra("acc2", AccountUtils.getAccount(PersonInfoActivity.this));
                    intent.putExtra("role2", AccountUtils.getRole(PersonInfoActivity.this));
                    intent.putExtra("name", AccountUtils.getName(PersonInfoActivity.this));
                    intent.putExtra("icon", AccountUtils.getIcon(PersonInfoActivity.this));
                }
            }
            startActivity(intent);
        }
    }
}
